package com.hkemmm.shop.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.hkemmm.alipay.bean.AliPayBean;
import com.hkemmm.alipay.service.IPayService;
import com.hkemmm.shop.entity.*;
import com.hkemmm.shop.service.*;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private IOrderService orderService;
    @Autowired
    private ICommentService commentService;
    @Autowired
    private ICarService carService;
    @Autowired
    private IGoodsService goodsService;
    @Autowired
    private IAddressService addressService;

    @Autowired
    private IPayService payService;


    @ResponseBody
    @RequestMapping("/getOrder")
    public List<OrderInfo> getOrderInfo(Integer uid, @RequestParam("state[]") int[] state, int start) {

        return orderService.getOrderInfo(uid, state, start);

    }

    @RequestMapping("/updateStatus")
    @ResponseBody
    public String updateOrderStatus(Integer id, Integer uid, Integer status) {
        int i = orderService.updateStatus(id, uid, status);
        if (i == 0) {
            return "err";
        }
        return "ok";
    }


    @RequestMapping("/getOrderComment")
    @ResponseBody
    public List<Order_detail> getOrderComment(Integer uid, Integer status) {
        return orderService.selectComment(uid, status);
    }

    @RequestMapping("/getNoOrderComment")
    @ResponseBody
    public List<Order_detail> getNoOrderComment(Integer uid) {
        return orderService.selectNoComment(uid);
    }


    @RequestMapping("/creatOrder")
    @ResponseBody
    public String creatOrder(@RequestParam("car") String c, @RequestParam("uid") Integer uid, HttpServletRequest request) {
        List<Car> carList = JSON.parseArray(c, Car.class);
        Float totalMoney = 0f;

        for (Car s1 : carList) {
            totalMoney += s1.getNumber() * s1.getPrice();
        }

        String code = Long.toString(System.currentTimeMillis());

        Order order = new Order();
        order.setAddressId(addressService.getOneAddress(uid).getId());
        order.setStatus(0);
        order.setAddtime(new Date());
        order.setIsdelete(0);
        order.setUserId(uid);
        order.setCode(code);
        order.setTotal(totalMoney);

        orderService.insertSelective(order);
        order = orderService.selectByCode(code);


        Order_detail orderDetail = new Order_detail();
        for (int i = 0; i < carList.size(); i++) {
            orderDetail.setOrderId(order.getId());
            orderDetail.setStatus(0);
            orderDetail.setTotal(carList.get(i).getPrice() * carList.get(i).getNumber());
            orderDetail.setGoodsId(carList.get(i).getGoodsId());
            orderDetail.setNum(carList.get(i).getNumber());
            orderService.insertSelective(orderDetail);
            carService.deleteByPrimaryKey(carList.get(i).getId());
        }

        return order.getId().toString();
    }

    @RequestMapping("/confirmOrder")
    public ModelAndView confirmOrder(Integer id, ModelAndView mav) {
        Order order = orderService.selectByPrimaryKey(id);
        List<Order_detail> orderDetails = orderService.selectByOrderId(id);


        mav.addObject("order", order);
        mav.addObject("orderGoods", orderDetails);
        mav.addObject("money", order.getTotal());
        mav.setViewName("/WEB-INF/pages/order");
        return mav;
    }


    @RequestMapping("/pay")
    public void aliPay(Integer id, Integer addressId, HttpServletRequest request, HttpServletResponse response) throws IOException, AlipayApiException {
        Order order = orderService.selectByPrimaryKey(id);
        order.setAddressId(addressId);
        order.setStatus(1);
        orderService.updateByPrimaryKeySelective(order);

        List<Order_detail> details = orderService.selectByOrderId(order.getId());
        String goods = "";
        for (Order_detail d : details) {
            goods += d.getGoods().getName() + "|";
        }

//        System.out.println(order.getCode());

        AliPayBean alipayBean = new AliPayBean();
        alipayBean.setSubject("鲜花时光:" + goods.substring(0, goods.length() - 1));
        alipayBean.setOut_trade_no(order.getCode());
        alipayBean.setTotal_amount(order.getTotal().toString());


        response.setContentType("text/html;charset=utf-8");
        String s = payService.aliPay(alipayBean);
        response.getWriter().write(s);//直接将完整的表单html输出到页面
        response.getWriter().flush();
        response.getWriter().close();
    }
}
